package com.chenwang.measure.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenwang.measure.MyApp;
import com.chenwang.measure.R;
import com.chenwang.measure.base.BaseActivity;
import com.chenwang.measure.model.bean.LocationBean;
import com.chenwang.measure.ui.adapter.CommandAdapter;
import com.chenwang.measure.utils.MyLogger;
import com.chenwang.measure.utils.MyToast;
import com.chenwang.measure.utils.RxTime;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
//开发测试界面
public class CommandActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {


    @BindView(R.id.et)
    EditText mEt;
    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.tv_state)
    TextView mTvState;
    private CommandAdapter mAdapter;
    private List<String> mList;
    private RxTime mRxTime;

    @Override
    public int setLayoutId() {
        return R.layout.activity_command;
    }


    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initRcycelrView();
        LocationBean.getInstance().setIs925(true);
       // LocationBean.getInstance().setOkMsg(true);
        mRxTime = new RxTime();
        mRxTime.interval(1000, new RxTime.RxAction() {
            @Override
            public void action(long number) {
                mTvState.setText(LocationBean.getInstance().getCosr()+"");
            }
        });

    }

    @OnClick(R.id.btn)
    public void onViewClicked() {
        String s = mEt.getText().toString();
        if (MyApp.getBluetoothSocket() != null) {
            try {
                MyApp.getBluetoothSocket().getOutputStream().write((s + "\r\n").getBytes());
            } catch (IOException e) {
                MyToast.show(e.getMessage());
            }

        }
    }
    private void initRcycelrView(){
        mList = new ArrayList<>();
        mList.add("cors---------------------");
        mList.add("set datalink gprs on");
        mList.add("tst gprsmsg on");
        mList.add("tst connect com2 gprs");
        mList.add("interfacemode gprs auto auto on");
        mList.add("set datalink gprs cors param hljcors.hl 192.189.3.7 2101 TCP Client");
        mList.add("set datalink gprs cors srctable RTCM32 ydkjgs1 kjgs4491");
        mList.add("saveconfig");
        mList.add("cors---------------------");
        mList.add("cors关闭------------------");
        mList.add("tst setcors disconnect");
        mList.add("cors关闭");
        mList.add("基本Log-------------------");
        mList.add("log com3 bestposb ontime 1");
        mList.add("log com3 m925b ontime 1");
        mList.add("log com3 satmsga ontime 1");
        mList.add("log com3 refstationb ontime 5");
        mList.add("log com3 psrdopb ontime 5");
        mList.add("基本Log--------------------");
        mList.add("reset");
        mList.add("freset");
        mList.add("unlogall");
        mList.add("电台-----------------------");
        mList.add("set datalink radio on");
        mList.add("interfacemode com2 auto auto on");
        mList.add("tst connect com2 $$0000A4600500132*46");
        mList.add("saveconfig");
        mList.add("电台-----------------------");
        mList.add("电台关闭--------------------");
        mList.add("set datalink radio off");
        mList.add("电台关闭--------------------");
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new CommandAdapter(mList);
        mAdapter.setOnItemClickListener(this);
        mRv.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (position==0) {
            LocationBean.getInstance().setIs925(false);
            LocationBean.getInstance().setOkMsg(true);
            MyToast.show(LocationBean.getInstance().getOk());
            MyLogger.i(LocationBean.getInstance().getOk());
            LocationBean.getInstance().setOk("");
        }else {
            if (MyApp.getBluetoothSocket() != null) {
                try {
                    MyApp.getBluetoothSocket().getOutputStream().write((mList.get(position) + "\r\n").getBytes());
                    MyToast.show("发送成功");
                } catch (IOException e) {
                    MyToast.show(e.getMessage());
                }

            } else {
                MyToast.show("未连接");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocationBean.getInstance().setIs925(false);
        LocationBean.getInstance().setOkMsg(false);
    }
}
